import { ServerConfig } from '../types';

export const config: ServerConfig = {
  port: parseInt(process.env.PORT || '3001', 10),
  host: process.env.HOST || '0.0.0.0',
  cors: {
    origin: [
      'https://mensanyun.com',
      'http://localhost:3000',
      'https://localhost:3000',
      'http://localhost:5173',
      'https://localhost:5173',
      'http://smartinput.com:3000',
      'https://smartinput.com:3000',
      'http://smartinput.com:5173',
      'https://smartinput.com:5173',
      'http://192.168.137.122:3000',
      'https://192.168.137.122:3000',
      'http://192.168.137.122:5173',
      'https://192.168.137.122:5173',
      'http://192.168.50.14:3000',
      'https://192.168.50.14:3000',
      'http://192.168.50.14:5173',
      'https://192.168.50.14:5173',
      'chrome-extension://*',
      'file://*'  // 支持本地文件测试
    ].concat(
      process.env.CORS_ORIGIN?.split(',') || [
        'http://localhost:3000',
        'https://localhost:3000',
        'http://localhost:5173',
        'https://localhost:5173',
        'http://smartinput.com:3000',
        'https://smartinput.com:3000',
        'http://smartinput.com:5173',
        'https://smartinput.com:5173',
        'http://192.168.137.122:3000',
        'https://192.168.137.122:3000',
        'http://192.168.137.122:5173',
        'https://192.168.137.122:5173',
        'http://192.168.50.14:3000',
        'https://192.168.50.14:3000',
        'http://192.168.50.14:5173',
        'https://192.168.50.14:5173',
        'chrome-extension://*',
        'file://*'  // 支持本地文件测试
      ]
    ),
    credentials: true
  },
  rateLimit: {
    windowMs: parseInt(process.env.RATE_LIMIT_WINDOW || '60000', 10), // 1分钟
    max: parseInt(process.env.RATE_LIMIT_MAX || '100', 10) // 每分钟最多100个请求
  },
  pairing: {
    codeLength: parseInt(process.env.PAIRING_CODE_LENGTH || '8', 10),
    expirationTime: parseInt(process.env.PAIRING_EXPIRATION || '300000', 10), // 5分钟
    maxAttempts: parseInt(process.env.PAIRING_MAX_ATTEMPTS || '3', 10),
    offlineTimeout: parseInt(process.env.PAIRING_OFFLINE_TIMEOUT || '60000', 10) // 60秒宽限期
  },
  cleanup: {
    deviceTimeout: parseInt(process.env.DEVICE_TIMEOUT || '300000', 10), // 5分钟
    pairingCodeCleanup: parseInt(process.env.PAIRING_CLEANUP || '60000', 10) // 1分钟
  }
};

// 环境变量验证
export function validateConfig(): void {
  const requiredEnvVars: string[] = [];
  
  // 检查必需的环境变量
  for (const envVar of requiredEnvVars) {
    if (!process.env[envVar]) {
      throw new Error(`Missing required environment variable: ${envVar}`);
    }
  }
  
  // 验证端口范围
  if (config.port < 1 || config.port > 65535) {
    throw new Error(`Invalid port number: ${config.port}. Must be between 1 and 65535.`);
  }
  
  // 验证配对码长度
  if (config.pairing.codeLength < 4 || config.pairing.codeLength > 10) {
    throw new Error(`Invalid pairing code length: ${config.pairing.codeLength}. Must be between 4 and 10.`);
  }
  
  console.log('✅ Configuration validated successfully');
}

// 开发环境配置
export const isDevelopment = process.env.NODE_ENV === 'development';
export const isProduction = process.env.NODE_ENV === 'production';
export const isTest = process.env.NODE_ENV === 'test';

// 日志配置
export const logConfig = {
  level: process.env.LOG_LEVEL || (isDevelopment ? 'debug' : 'info'),
  enableConsole: process.env.LOG_CONSOLE !== 'false',
  enableFile: process.env.LOG_FILE === 'true',
  filePath: process.env.LOG_FILE_PATH || './logs/smartinput.log'
};

// 安全配置
export const securityConfig = {
  enableHelmet: process.env.SECURITY_HELMET !== 'false',
  enableRateLimit: process.env.SECURITY_RATE_LIMIT !== 'false',
  trustProxy: process.env.TRUST_PROXY === 'true',
  sessionSecret: process.env.SESSION_SECRET || 'smartinput-dev-secret-change-in-production'
};

export default config;
